package com.shrct.businessprocess.money;

import com.google.common.base.Objects;
import com.shrct.businessprocess.common.CurrencyType;
import com.shrct.businessprocess.project.RCExchangeRate;
import com.shrct.businessprocess.project.RCProjectMoney;
import com.shrct.businessprocess.project.RCRealMoney;
import org.eclipse.xtext.xbase.lib.DoubleExtensions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.Functions.Function2;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;

@SuppressWarnings("all")
public class Moneys {
  public static RCProjectMoney sum(final Iterable<RCProjectMoney> aLotOfMoney, final RCExchangeRate exchangeRate) {
    RCProjectMoney _xblockexpression = null;
    {
      final Function2<RCProjectMoney,RCProjectMoney,RCProjectMoney> _function = new Function2<RCProjectMoney,RCProjectMoney,RCProjectMoney>() {
          public RCProjectMoney apply(final RCProjectMoney m1, final RCProjectMoney m2) {
            RCProjectMoney _plus = m1.plus(m2);
            return _plus;
          }
        };
      RCProjectMoney result = IterableExtensions.<RCProjectMoney>reduce(aLotOfMoney, _function);
      RCProjectMoney _xifexpression = null;
      boolean _equals = Objects.equal(result, null);
      if (_equals) {
        RCProjectMoney _rCProjectMoney = new RCProjectMoney();
        final Procedure1<RCProjectMoney> _function_1 = new Procedure1<RCProjectMoney>() {
            public void apply(final RCProjectMoney it) {
              it.setAmount(Double.valueOf(0d));
              it.setExchange(exchangeRate);
            }
          };
        RCProjectMoney _doubleArrow = ObjectExtensions.<RCProjectMoney>operator_doubleArrow(_rCProjectMoney, _function_1);
        _xifexpression = _doubleArrow;
      } else {
        RCProjectMoney _money = result.toMoney(exchangeRate);
        _xifexpression = _money;
      }
      _xblockexpression = (_xifexpression);
    }
    return _xblockexpression;
  }
  
  public static RCRealMoney sumRealMoney(final Iterable<RCRealMoney> aLotOfMoney, final RCExchangeRate de) {
    RCRealMoney _xblockexpression = null;
    {
      final Function1<RCRealMoney,Double> _function = new Function1<RCRealMoney,Double>() {
          public Double apply(final RCRealMoney it) {
            Double _hostAmount = it.toHostAmount();
            return _hostAmount;
          }
        };
      Iterable<Double> _map = IterableExtensions.<RCRealMoney, Double>map(aLotOfMoney, _function);
      final Function2<Double,Double,Double> _function_1 = new Function2<Double,Double,Double>() {
          public Double apply(final Double m1, final Double m2) {
            double _plus = DoubleExtensions.operator_plus(m1, m2);
            return Double.valueOf(_plus);
          }
        };
      final Double result = IterableExtensions.<Double>reduce(_map, _function_1);
      RCRealMoney _xifexpression = null;
      boolean _equals = Objects.equal(result, null);
      if (_equals) {
        RCRealMoney _rCRealMoney = new RCRealMoney();
        final Procedure1<RCRealMoney> _function_2 = new Procedure1<RCRealMoney>() {
            public void apply(final RCRealMoney it) {
              it.setAmount(Double.valueOf(0d));
              it.setExchangeRate(Double.valueOf(1d));
              CurrencyType _currencyType = de.getCurrencyType();
              it.setCurrencyType(_currencyType);
            }
          };
        RCRealMoney _doubleArrow = ObjectExtensions.<RCRealMoney>operator_doubleArrow(_rCRealMoney, _function_2);
        _xifexpression = _doubleArrow;
      } else {
        RCRealMoney _rCRealMoney_1 = new RCRealMoney();
        final Procedure1<RCRealMoney> _function_3 = new Procedure1<RCRealMoney>() {
            public void apply(final RCRealMoney it) {
              Double _rate = de.getRate();
              double _divide = DoubleExtensions.operator_divide(result, _rate);
              it.setAmount(Double.valueOf(_divide));
              it.setExchangeRate(Double.valueOf(1d));
              CurrencyType _currencyType = de.getCurrencyType();
              it.setCurrencyType(_currencyType);
            }
          };
        RCRealMoney _doubleArrow_1 = ObjectExtensions.<RCRealMoney>operator_doubleArrow(_rCRealMoney_1, _function_3);
        _xifexpression = _doubleArrow_1;
      }
      _xblockexpression = (_xifexpression);
    }
    return _xblockexpression;
  }
  
  public static RCRealMoney sumHostRealMoney(final Iterable<Double> aLotOfMoney, final RCExchangeRate de) {
    RCRealMoney _xblockexpression = null;
    {
      final Function2<Double,Double,Double> _function = new Function2<Double,Double,Double>() {
          public Double apply(final Double m1, final Double m2) {
            double _plus = DoubleExtensions.operator_plus(m1, m2);
            return Double.valueOf(_plus);
          }
        };
      final Double result = IterableExtensions.<Double>reduce(aLotOfMoney, _function);
      RCRealMoney _xifexpression = null;
      boolean _equals = Objects.equal(result, null);
      if (_equals) {
        RCRealMoney _rCRealMoney = new RCRealMoney();
        final Procedure1<RCRealMoney> _function_1 = new Procedure1<RCRealMoney>() {
            public void apply(final RCRealMoney it) {
              it.setAmount(Double.valueOf(0d));
              it.setExchangeRate(Double.valueOf(1d));
              CurrencyType _currencyType = de.getCurrencyType();
              it.setCurrencyType(_currencyType);
            }
          };
        RCRealMoney _doubleArrow = ObjectExtensions.<RCRealMoney>operator_doubleArrow(_rCRealMoney, _function_1);
        _xifexpression = _doubleArrow;
      } else {
        RCRealMoney _rCRealMoney_1 = new RCRealMoney();
        final Procedure1<RCRealMoney> _function_2 = new Procedure1<RCRealMoney>() {
            public void apply(final RCRealMoney it) {
              Double _rate = de.getRate();
              double _divide = DoubleExtensions.operator_divide(result, _rate);
              it.setAmount(Double.valueOf(_divide));
              it.setExchangeRate(Double.valueOf(1d));
              CurrencyType _currencyType = de.getCurrencyType();
              it.setCurrencyType(_currencyType);
            }
          };
        RCRealMoney _doubleArrow_1 = ObjectExtensions.<RCRealMoney>operator_doubleArrow(_rCRealMoney_1, _function_2);
        _xifexpression = _doubleArrow_1;
      }
      _xblockexpression = (_xifexpression);
    }
    return _xblockexpression;
  }
}
